package br.unifor.cct.mia.evolutionary;

import br.unifor.cct.mia.ga.util.FeatureConstructionGene;
import br.unifor.cct.mia.util.Configurations;
import br.unifor.cct.mia.util.Methods;

public class FeatureConstructionGenotype  extends Individual {

	private static final long serialVersionUID = -8607596445277848309L;

	protected Configurations configurations;
	protected double selectProbability;
	protected FeatureConstructionGene[] gene;
		
	public FeatureConstructionGenotype(Configurations c){
		this.configurations = c;
		gene = new FeatureConstructionGene[c.getNvars()];
	}
	
	public Object clone() {
		FeatureConstructionGenotype result = new FeatureConstructionGenotype(configurations);		
		result.setFitness(getFitness());
		result.setSelectProbability(this.getSelectProbability());
	    System.arraycopy(this.getGene(),0,result.getGene(),0,this.getGene().length);
		return result;
	}
	
	public String toString(){
		String genes = "";
		
		for(int i = 0; i < configurations.getNvars(); i++) 
			genes += gene[i]+"  ";
		
		return  "fitness = "+this.getFitness()+"\n" +
				"probabilty = "+selectProbability+"\n"+
				"genes = "+genes;
	}
		
	public int hashCode() {
		int hashCode = 0;
		int atual = 3;
		for ( int i=0; i<gene.length; i++ ) {
			int primo = Methods.gerarPrimo(atual);
			hashCode += gene[i].hashCode() * primo;				
			atual++;
		}		
		return hashCode;
	}
	
	public boolean equals(Object arg0) {
		FeatureConstructionGenotype gen = (FeatureConstructionGenotype)arg0;
		
		for ( int i=0; i<gene.length; i++ ) {
			if ( !gen.getGene()[i].getAttribute().equals(gene[i].getAttribute()) ||
					!gen.getGene()[i].getValue().equals(gene[i].getValue()) ||
					!gen.getGene()[i].getOperator().equals(gene[i].getOperator()) || 
					gen.getGene()[i].getActiveBit() != gene[i].getActiveBit() ) 
				return false;
		}
		return true;
	}	
	
	public double getSelectProbability() {
		return selectProbability;
	}
	public void setSelectProbability(double rfitness) {
		this.selectProbability = rfitness;
	}    
    public FeatureConstructionGene[] getGene() {
		return gene;
	}
	public void setGene(FeatureConstructionGene[] gene) {
		this.gene = gene;
	}
	public double getFitnessMin() {
		return -getFitness();
	}

}
